package copyengine.utils.delayEffect.effect.basic
{
	import flash.display.DisplayObjectContainer;
	import copyengine.utils.delayEffect.effect.ICEDelayEffect;

	/**
	 * <b>当前Effect特性为:</b></br>
	 * <b>包含子Child的Delayeffect,且子Child是一同执行,全部Child执行完毕则当前Effect执行完毕</b>
	 * </br></br>===========================</br></br>
	 * <b>所有Effect特性为:</b>
	 * <li>BasicEffect 					不包含子Child,可以Override,当Execute时候->主逻辑执行完成及为完成
	 * <li>MacroEffect 					包含子Child,不可以Override,当ExecuteBranch时候->所有子Child一同执行
	 * <li>ContianBranchEffect		包含子Child,可以Override,当ExecuteBranch时候->让所有Branch按顺序执行
	 * <li>SelectableChildEffect	包含子Child,可以Override,当ExecuteBranch时候->选择应该执行哪个Branch
	 */
	public final class CEDelayMacroEffect implements ICEDelayEffect
	{
		private var flowFinishCallBack:Function;
		private var executeFinishCallBack:Function;

		private var subDelayEffectVector:Vector.<ICEDelayEffect>;

		private var subFlowFinishNum:int = 0;
		private var subExecuteFinishNum:int = 0;

		public final function CEDelayMacroEffect()
		{
			subDelayEffectVector = new Vector.<ICEDelayEffect>();
			super();
		}

		public function addSubDelayEffect(_delayEffect:ICEDelayEffect):void
		{
			subDelayEffectVector.push(_delayEffect);
		}

		public final function setLayer(_sceneUILayer:DisplayObjectContainer, _sceneLayer:DisplayObjectContainer, _globalUILayer:DisplayObjectContainer):void
		{
			for each (var effect:ICEDelayEffect in subDelayEffectVector)
			{
				effect.setLayer(_sceneUILayer, _sceneLayer, _globalUILayer);
			}
		}

		public function execute(_flowFinishCallBack:Function, _executeFinishCallBack:Function):void
		{
			flowFinishCallBack = _flowFinishCallBack;
			executeFinishCallBack = _executeFinishCallBack;
			for each (var effect:ICEDelayEffect in subDelayEffectVector)
			{
				effect.execute(onSubEffectFlowFinish, onSubEffectExecuteFinish);
			}
			//没有subEffect
			if (subDelayEffectVector.length == 0)
			{
				flowFinishCallBack && flowFinishCallBack();
				flowFinishCallBack = null;

				executeFinishCallBack && executeFinishCallBack();
				executeFinishCallBack = null;
			}
		}

		public function dispose():void
		{
			executeFinishCallBack = null;
			flowFinishCallBack = null;

			while (subDelayEffectVector.length > 0)
			{
				subDelayEffectVector.pop().dispose();
			}
		}

		public function isExecuteFinish():Boolean
		{
			return subExecuteFinishNum >= subDelayEffectVector.length;
		}

		public function finishExecuteImmediately():void
		{
			for each (var effect:ICEDelayEffect in subDelayEffectVector)
			{
				effect.finishExecuteImmediately();
			}
		}

		public function onFlowExecuteFinish():void
		{
			for each (var effect:ICEDelayEffect in subDelayEffectVector)
			{
				effect.onFlowExecuteFinish();
			}
		}

		private function onSubEffectFlowFinish():void
		{
			subFlowFinishNum++;
			if (subFlowFinishNum >= subDelayEffectVector.length)
			{
				flowFinishCallBack && flowFinishCallBack();
				flowFinishCallBack = null;
			}
		}

		private function onSubEffectExecuteFinish():void
		{
			subExecuteFinishNum++;
			if (subExecuteFinishNum >= subDelayEffectVector.length)
			{
				executeFinishCallBack && executeFinishCallBack();
				executeFinishCallBack = null;
			}
		}

	}
}
